//引入获取商品的请求方法
import * as api from '@/api'

export default {
    namespaced: true,
    state: {
        productData: [
            {
                id: 1,
                inventory: 200,
                price: 500.01,
                title: "小霸王游戏机"
            },
            {
                id: 2,
                inventory: 100,
                price: 10.99,
                title: "香奈儿白色T恤"
            },
            {
                id: 3,
                inventory: 50,
                price: 19.99,
                title: "卓依婷新年专辑"
            },
        ]
    },
    mutations: {
        //更改productData的mutation
        SET_PRODUC_DATAT(state, payload) {
            state.productData = payload
        },
        // 让库存减少
        DECREASE_INVENTORY(state, payload) {
            // payload接收的是库存减少这条数据的id
            console.log(payload);
            // 通过payload到productData中查找对应的数据,让inventory-1
            let target = state.productData.find(item => item.id === payload)
            if (target) {
                target.inventory--
            }
        }
    },
    actions: {
        //使用action获取异步数据
        async FETCH_PRODUCT(context) {
            let res = await api.fetchProduct()
            console.log(res);
            context.commit('SET_PRODUC_DATAT', res.data)
        }
    },
    getters: {}
}